package common.post.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import common.customer.bean.Customer;
import common.customer.service.CustomerService;
import common.post.bean.Contacts;
import common.post.bean.Post;
import common.post.bean.SInfo;
import common.post.bean.SInfoComment;
import common.post.service.ContactsService;
import common.post.service.PostService;
import common.post.service.SInfoCommentService;
import common.post.service.SInfoService;
import common.system.bean.Area;
import common.system.bean.City;
import common.system.service.AreaService;
import common.system.service.CityService;
import common.system.util.SystemUtil;
import common.util.CommonCode;
import common.util.StrutsUtil;

public class SInfoAction extends ActionSupport {
	private static final long serialVersionUID = 2654523474332883745L;
	/**
	 * 增加s info
	 * @return
	 */
	private ContactsService contactsService;
	private SInfoService sInfoService;
	private SInfoCommentService sInfoCommentService;
	private CityService cityService;
	private AreaService areaService;
	private CustomerService customerService;
	private PostService postService;
	private String type;
	private String title;
	private String phone;
	private String email;
	private String qq;
	private String desc;
	private String images;
	private int areaId;
	private List<SInfo> list;
	private List<SInfoComment> sinfoComments;
	private List<Post> posts;
	private String resultStr;
	private String resultCode;
	
	private String validationDate;
	
	private int sinfoId;
	private SInfo sinfo;
	
	private int page;//页码
	private int pageSize;//每页大小
	private int total;//总共数量
	private int totalPage;//总共页数
	private int cityId;//城市id
	private int days;//发布天数
	private String orderBy;//按照什么排序
	private String jinghua;//只选择精华
	private String tuijian;//是否推荐
	
	private List<Area> areas;
	private String validationCode;
	
	private String replyContent;//回复的字串
	
	private long commentId;
	
	public String replySinfoComment(){
		if(commentId<=0){
			return "notfound";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(this.sInfoCommentService.addSInfoCommentReply(commentId, replyContent, user)){			
			return SUCCESS;
		}else{
			resultStr ="保存帖子回复信息发生错误";
			resultCode = "0";
			return "error";
		}
	}
	public String reply(){
		if(sinfoId<=0){
			return "notfound";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		Customer user = (Customer)session.getAttribute("user");
		if(this.sInfoCommentService.addSInfoComment(sinfoId, replyContent, user)){			
			return SUCCESS;
		}else{
			resultStr ="保存帖子回复信息发生错误";
			resultCode = "0";
			return "error";
		}
	}
	public String publishNewGirl(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		int currentCityId = StrutsUtil.getCurrentCityBySession(session);
		areas = areaService.getAreaByCityId(currentCityId);
		return SUCCESS;
	}
	public String addNewGril(){
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		Customer customer = (Customer)user;
		if(user==null){
			resultStr ="必须登录之后才能发布信息";
			resultCode = "0";
			return "notlogin";
		}
		String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
		if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
			resultStr ="验证码不存在,请重新换一个验证码";
			resultCode = "0";
			System.out.println("验证码不存在,请重新换一个验证码");
			HttpSession session = ServletActionContext.getRequest().getSession();
			int currentCityId = StrutsUtil.getCurrentCityBySession(session);
			areas = areaService.getAreaByCityId(currentCityId);
			return "validateCodeError";
		}
		if(!validationCodeSession.equals(validationCode)){
			resultStr ="验证码输入有误,请重新输入";
			resultCode = "0";
			System.out.println(validationCodeSession+"="+validationCode);
			System.out.println("验证码输入有误,请重新输入");
			HttpSession session = ServletActionContext.getRequest().getSession();
			int currentCityId = StrutsUtil.getCurrentCityBySession(session);
			areas = areaService.getAreaByCityId(currentCityId);
			return "validateCodeError";
		}
		if(SystemUtil.validateAllNull(qq)){
			resultStr ="QQ不能为空";
			resultCode = "0";
			return "validateCodeError";
		}
		if(SystemUtil.validateAllNull(phone)){
			resultStr ="手机不能为空";
			resultCode = "0";
			return "validateCodeError";
		}
		if(title==null || "".equals(title.trim())){
			resultStr ="信息标题必填";
			resultCode = "0";
			return "validateCodeError";
		}
		City city = null;
		Area area = null;
		if(cityId==0){
			Object c = ServletActionContext.getRequest().getSession().getAttribute("city");
			if(c!=null){
				city = (City)c;
			}else{
				Object u = ServletActionContext.getRequest().getSession().getAttribute("user");
				if(u!=null){
					city = ((Customer)u).getCity();
				}else{
					city = cityService.getCityByID(1);
				}
			}
		}else{
			city = cityService.getCityByID(cityId);
		}
		if(city==null){
			resultStr ="加载所在城市出错";
			resultCode = "0";
			return SUCCESS;
		}
		if(areaId==0){
			resultStr ="区县必须选择";
			resultCode = "0";
			return SUCCESS;
		}else{
			area = areaService.getAreaByID(areaId);
		}
		if(area==null){
			resultStr ="加载所在城市出错";
			resultCode = "0";
			return SUCCESS;
		}
		sinfoId = this.sInfoService.addNewGirl(city, area, customer, desc, images, title, phone, qq);
		if(sinfoId>0){
			resultCode="1";
			return SUCCESS;
		}else{
			resultCode="0";
			resultStr ="发布信息发生错误，请联系管理员：错误码，数据入库出错！";
			return "error";
		}
	}
	//发布sinfo信息
	public String publishSinfo(){
		/*Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user==null){
			return "notlogin";
		}*/
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		int currentCityId = StrutsUtil.getCurrentCityBySession(session);
		areas = areaService.getAreaByCityId(currentCityId);
		return SUCCESS;
	}
	//根据ID获得楼凤信息
	public String getSInfoById(){
		if(sinfoId==0){
			return "notfound";
		}
		sinfo=this.sInfoService.getSInfoById(sinfoId);
		String pics = sinfo.getPics();
		List<String> photos = new ArrayList<String>();
		if(pics!=null && !"".equals(pics.trim())){
			String[] ps = pics.split(",");
			for(String p:ps){
				if(p!=null && !"".equals(p.trim())){
					photos.add(p);
				}
			}
		}
		sinfo.setPhotos(photos);
		sinfo.setPublishDate(StrutsUtil.getFormat("yyyy-MM-dd HH:mm").format(new Date(sinfo.getPublishTime())));
		total = (int)this.sInfoCommentService.getSInfoCommentCountBySInfoId(sinfoId);
		if(pageSize <= 0){
			pageSize=10;
		}
		totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
		if(page<1){
			page=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		sinfoComments = this.sInfoCommentService.getSInfoCommentBySInfoId(sinfoId, page, pageSize);
		return SUCCESS;
	}
	/**
	 * 获得当前城市下的所有信息
	 * @return
	 */
	public String getAllLoufeng(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		int currentCityId =0;
		//当前城市
		if(cityId==0){			
			currentCityId = StrutsUtil.getCurrentCityBySession(session);
		}else{
			currentCityId =cityId;
		}
		areas = areaService.getAreaByCityId(currentCityId);
		Customer user = (Customer)ServletActionContext.getRequest().getSession().getAttribute("user");
		
		if(user!=null && user.isVip()){
			//获得总量
			total = (int)this.sInfoService.countSInfo(currentCityId, areaId,
					CommonCode.SINFO_TYPE_LOUFENG,
					days,jinghua, tuijian,true);
		}else{
			//获得总量
			total = (int)this.sInfoService.countSInfo(currentCityId, areaId,
					CommonCode.SINFO_TYPE_LOUFENG,
					days,jinghua, tuijian,true);
		}
		if(pageSize <= 0){
			pageSize=20;
		}
		totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
		if(page<1){
			page=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(user !=null && user.isVip()){
			//获得列表
			list = this.sInfoService.getSInfos(currentCityId, areaId, 
											CommonCode.SINFO_TYPE_LOUFENG, 
											page, pageSize, days,
											orderBy, jinghua, tuijian,true);
			if(list!=null){
				for(SInfo s:list){
					s.setPublishDate(sdf.format(new Date(s.getPublishTime())));
				}
			}
			
		}else{
			//获得列表
			list = this.sInfoService.getSInfos(currentCityId, areaId, 
											CommonCode.SINFO_TYPE_LOUFENG, 
											page, pageSize, days,
											orderBy, jinghua, tuijian,true);
			if(list!=null){
				for(SInfo s:list){
					s.setPublishDate(sdf.format(new Date(s.getPublishTime())));
				}
			}
		}
		posts = this.postService.getGonggaoByTypeAndChannel(CommonCode.POST_TYPE_GONGGAO,CommonCode.SYSTEM_CHANNEL_SHEQUNVSHENG);
		return SUCCESS;
	}
	/**
	 * 获得当前城市下的所有信息
	 * @return
	 */
	public String getAllExcludeLoufeng(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		int currentCityId =0;
		//当前城市
		if(cityId==0){			
			currentCityId = StrutsUtil.getCurrentCityBySession(session);
		}else{
			currentCityId =cityId;
		}
		areas = areaService.getAreaByCityId(currentCityId);
		Customer user = (Customer)ServletActionContext.getRequest().getSession().getAttribute("user");
		
		if(user!=null && user.isVip()){
			//获得总量
			total = (int)this.sInfoService.countSInfoExcludeType(currentCityId, areaId,
					CommonCode.SINFO_TYPE_LOUFENG,
					days,jinghua, tuijian,true);
		}else{
			//获得总量
			total = (int)this.sInfoService.countSInfoExcludeType(currentCityId, areaId,
					CommonCode.SINFO_TYPE_LOUFENG,
					days,jinghua, tuijian,true);
		}
		if(pageSize <= 0){
			pageSize=20;
		}
		totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
		if(page<1){
			page=1;
		}
		if(page>totalPage){
			page=totalPage;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(user !=null && user.isVip()){
			//获得列表
			list = this.sInfoService.getSInfosExcludeType(currentCityId, areaId, 
											CommonCode.SINFO_TYPE_LOUFENG, 
											page, pageSize, days,
											orderBy, jinghua, tuijian,true);
			if(list!=null){
				for(SInfo s:list){
					s.setPublishDate(sdf.format(new Date(s.getPublishTime())));
				}
			}
			
		}else{
			//获得列表
			list = this.sInfoService.getSInfosExcludeType(currentCityId, areaId, 
											CommonCode.SINFO_TYPE_LOUFENG, 
											page, pageSize, days,
											orderBy, jinghua, tuijian,true);
			if(list!=null){
				for(SInfo s:list){
					s.setPublishDate(sdf.format(new Date(s.getPublishTime())));
				}
			}
		}
		posts = this.postService.getGonggaoByTypeAndChannel(CommonCode.POST_TYPE_GONGGAO,CommonCode.SYSTEM_CHANNEL_HUISUO);
		return SUCCESS;
	}
	/**
	 * 新增sinfo
	 * @return
	 */
	public String addSInfo(){
		//System.out.println(this.getText("file.upload.path"));
		//System.out.println(StrutsConstants.STRUTS_ACTION_EXCLUDE_PATTERN);
		Object user = ServletActionContext.getRequest().getSession().getAttribute("user");
		Customer customer = (Customer)user;
		if(user==null){
			resultStr ="必须登录之后才能发布信息";
			resultCode = "0";
			return "notlogin";
		}
		String validationCodeSession= ServletActionContext.getRequest().getSession().getAttribute("validationCode").toString(); 
		if(validationCodeSession==null || "".equals(validationCodeSession.trim())){
			resultStr ="验证码不存在,请重新换一个验证码";
			resultCode = "0";
			System.out.println("验证码不存在,请重新换一个验证码");
			HttpSession session = ServletActionContext.getRequest().getSession();
			int currentCityId = StrutsUtil.getCurrentCityBySession(session);
			areas = areaService.getAreaByCityId(currentCityId);
			return "validateCodeError";
		}
		if(!validationCodeSession.equals(validationCode)){
			resultStr ="验证码输入有误,请重新输入";
			resultCode = "0";
			System.out.println(validationCodeSession+"="+validationCode);
			System.out.println("验证码输入有误,请重新输入");
			HttpSession session = ServletActionContext.getRequest().getSession();
			int currentCityId = StrutsUtil.getCurrentCityBySession(session);
			areas = areaService.getAreaByCityId(currentCityId);
			return "validateCodeError";
		}
		if(SystemUtil.validateAllNull(phone,email,qq)){
			resultStr ="手机，邮件，QQ不能都不填";
			resultCode = "0";
			return "validateCodeError";
		}
		if(type==null || "".equals(type.trim()) || "0".equals(type.trim())){
			resultStr ="信息类型必须选择";
			resultCode = "0";
			return "validateCodeError";
		}
		if(title==null || "".equals(title.trim())){
			resultStr ="信息标题必填";
			resultCode = "0";
			return "validateCodeError";
		}
		if(CommonCode.SINFO_TYPE_LOUFENG.endsWith(type.trim())){
			if(qq==null || "".equals(qq.trim())){
				resultStr ="楼凤必须填QQ";
				resultCode = "0";
				return "validateCodeError";
			}
		}
		City city = null;
		Area area = null;
		if(cityId==0){
			Object c = ServletActionContext.getRequest().getSession().getAttribute("city");
			if(c!=null){
				city = (City)c;
			}else{
				Object u = ServletActionContext.getRequest().getSession().getAttribute("user");
				if(u!=null){
					city = ((Customer)u).getCity();
				}else{
					city = cityService.getCityByID(1);
				}
			}
		}else{
			city = cityService.getCityByID(cityId);
		}
		if(city==null){
			resultStr ="加载所在城市出错";
			resultCode = "0";
			return SUCCESS;
		}
		if(areaId==0){
			resultStr ="区县必须选择";
			resultCode = "0";
			return SUCCESS;
		}else{
			area = areaService.getAreaByID(areaId);
		}
		if(area==null){
			resultStr ="加载所在城市出错";
			resultCode = "0";
			return SUCCESS;
		}
		Set<Contacts> set = new HashSet<Contacts>();
		if(phone!=null && !"".equals(phone.trim())){
			Contacts c = new Contacts();
			c.setContent(phone);
			c.setTypeName(CommonCode.CONNTCTS_TYPE_TELPHONE);
			boolean b=contactsService.addContacts(c);
			if(b){
				set.add(c);				
			}
		}
		if(email!=null && !"".equals(email.trim())){
			Contacts c = new Contacts();
			c.setContent(email);
			c.setTypeName(CommonCode.CONNTCTS_TYPE_EMAIL);
			boolean b=contactsService.addContacts(c);
			if(b){
				set.add(c);				
			}
		}
		if(qq!=null && !"".equals(qq.trim())){
			Contacts c = new Contacts();
			c.setContent(qq);
			c.setTypeName(CommonCode.CONNTCTS_TYPE_QQ);
			boolean b=contactsService.addContacts(c);
			if(b){
				set.add(c);				
			}
		}
		
		SInfo sinfo =new SInfo();
		sinfo.setType(type);
		sinfo.setContent(desc);
		sinfo.setPics(images);
		sinfo.setContacts(set);
		sinfo.setCity(city);
		sinfo.setArea(area);
		sinfo.setTitle(title);
		sinfo.setPublisher(customer);
		sinfo.setVip(false);
		boolean result = sInfoService.addSInfo(sinfo);
		if(result){
			resultCode="1";
			return SUCCESS;
		}else{
			resultCode="0";
			resultStr ="发布信息发生错误，请联系管理员：错误码，数据入库出错！";
			return SUCCESS;
		}
	}
	//===========getter和setter方法=============
	
	public SInfoService getsInfoService() {
		return sInfoService;
	}
	public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public ContactsService getContactsService() {
		return contactsService;
	}

	public void setContactsService(ContactsService contactsService) {
		this.contactsService = contactsService;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public int getAreaId() {
		return areaId;
	}

	public void setAreaId(int areaId) {
		this.areaId = areaId;
	}

	public void setsInfoService(SInfoService sInfoService) {
		this.sInfoService = sInfoService;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public String getImages() {
		return images;
	}
	public void setImages(String images) {
		this.images = images;
	}
	public String getResultStr() {
		return resultStr;
	}
	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}
	public String getResultCode() {
		return resultCode;
	}
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}
	public List<SInfo> getList() {
		return list;
	}
	public void setList(List<SInfo> list) {
		this.list = list;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getDays() {
		return days;
	}
	public void setDays(int days) {
		this.days = days;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public String getJinghua() {
		return jinghua;
	}
	public void setJinghua(String jinghua) {
		this.jinghua = jinghua;
	}
	public String getTuijian() {
		return tuijian;
	}
	public void setTuijian(String tuijian) {
		this.tuijian = tuijian;
	}
	public int getSinfoId() {
		return sinfoId;
	}
	public void setSinfoId(int sinfoId) {
		this.sinfoId = sinfoId;
	}
	public SInfo getSinfo() {
		return sinfo;
	}
	public void setSinfo(SInfo sinfo) {
		this.sinfo = sinfo;
	}
	public List<Area> getAreas() {
		return areas;
	}
	public void setAreas(List<Area> areas) {
		this.areas = areas;
	}
	public String getValidationCode() {
		return validationCode;
	}
	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}
	public CustomerService getCustomerService() {
		return customerService;
	}
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	public String getValidationDate() {
		return validationDate;
	}
	public void setValidationDate(String validationDate) {
		this.validationDate = validationDate;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public List<Post> getPosts() {
		return posts;
	}
	public void setPosts(List<Post> posts) {
		this.posts = posts;
	}
	public PostService getPostService() {
		return postService;
	}
	public void setPostService(PostService postService) {
		this.postService = postService;
	}
	public SInfoCommentService getsInfoCommentService() {
		return sInfoCommentService;
	}
	public void setsInfoCommentService(SInfoCommentService sInfoCommentService) {
		this.sInfoCommentService = sInfoCommentService;
	}
	public List<SInfoComment> getSinfoComments() {
		return sinfoComments;
	}
	public void setSinfoComments(List<SInfoComment> sinfoComments) {
		this.sinfoComments = sinfoComments;
	}
	public String getReplyContent() {
		return replyContent;
	}
	public void setReplyContent(String replyContent) {
		this.replyContent = replyContent;
	}
	public long getCommentId() {
		return commentId;
	}
	public void setCommentId(long commentId) {
		this.commentId = commentId;
	}
	
}
